package com.example.bai13;

import java.util.ArrayList;

import com.example.object.MyContact;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
           
        }
      
    }
    
   
   
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }
        EditText edtname,edtnumber;
    	Button btnsave;
    	ListView list;
    	ArrayList<MyContact> arr;
    	ArrayAdapter<MyContact> adapter;
    	MyContact ctselect=null;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
        @Override
		public void onStart() {
			// TODO Auto-generated method stub
			super.onStart();
			getControls();
			addEvents();
		}
		private void getControls()
        {
        	 edtname= (EditText) this.getActivity().findViewById(R.id.edtname);
        	 edtnumber= (EditText) this.getActivity().findViewById(R.id.edtnumber);
        	 btnsave= (Button) this.getActivity().findViewById(R.id.btnsave);
        	 list= (ListView) this.getActivity().findViewById(R.id.list);
        	 arr= new ArrayList<MyContact>();
        	 adapter = new ArrayAdapter<MyContact>(this.getActivity(), android.R.layout.simple_list_item_1,arr);
        	 list.setAdapter(adapter);
        	 
        	 registerForContextMenu(list);
        }
        private void addEvents()
        {
        	btnsave.setOnClickListener(new View.OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				proccessSaveContact();
    			}
    		});
        	list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    			@Override
    			public boolean onItemLongClick(AdapterView<?> parent, View view,
    					int position, long id) {
    				// TODO Auto-generated method stub
    				ctselect=arr.get(position);
    				return false;
    			}
    		});
        }
        
        private void proccessSaveContact()
        {
        	MyContact ct= new MyContact();
        	ct.setName(edtname.getText().toString());
        	ct.setPhone(edtnumber.getText().toString());
        	arr.add(ct);
        	adapter.notifyDataSetChanged();
        }
        private void proccessRemoveContact()
    	{
    		arr.remove(ctselect);
    		adapter.notifyDataSetChanged();
    	}
    	private void proccessCall()
    	{
    		Uri uri=Uri.parse("tel:"+ctselect.getPhone());
    		Intent i= new Intent(Intent.ACTION_CALL,uri);
    		getActivity().startActivity(i);
    	}
    	private void proccessSendSMS()
    	{
    		Intent i= new Intent(this.getActivity(),SendsmsActivity.class );
    		Bundle b= new Bundle();
    		b.putSerializable("CONTACT", ctselect);
    		i.putExtra("DATA", b);
    		getActivity().startActivity(i);
    	}
    	 

    	    @Override
    		public void onCreateContextMenu(ContextMenu menu, View v,
    				ContextMenuInfo menuInfo) {
    			// TODO Auto-generated method stub
    			super.onCreateContextMenu(menu, v, menuInfo);
    			this.getActivity().getMenuInflater().inflate(R.menu.mn_on_click_item_list, menu);
    			for(int i=0;i<menu.size();i++)
    			{	
    			String tmp= menu.getItem(i).getTitle().toString();
    			menu.getItem(i).setTitle(tmp+" "+ctselect.toString());
    			}
    		}

    		@Override
    		public boolean onContextItemSelected(MenuItem item) {
    			// TODO Auto-generated method stub
    			switch(item.getItemId())
    			{
    				case R.id.itemcall:
    				{
    					proccessCall();
    					break;
    				}
    				case R.id.itemsendsms:
    				{
    					proccessSendSMS();
    					break;
    				}
    				case R.id.itemremove:
    				{
    					proccessRemoveContact();
    					break;
    				}
    			}
    			return super.onContextItemSelected(item);
    		}
    		
        
    }

}
